package Cluster;

import java.util.ArrayList;

import PaperExperiment.DTW;
import PaperExperiment.LCSSdistance;

public class Matric {

	public static final int MATRIC_DTW = 0;
	public static final int MATRIC_LCSS = 1;
	
	public static double maxDist = 0;
	
	public static double MDist(int matricType, Trajectory traj1, Trajectory traj2) {
		double result = 0.0;
		switch(matricType) {
		case Matric.MATRIC_DTW :
			result =  Matric.DTW(traj1, traj2);
			break;
		case MATRIC_LCSS :
			result = Matric.LCSS(traj1, traj2, 40);
			break;
		}
		return result;
	}
	
	public static double DTW(Trajectory traj1, Trajectory traj2) {
		double likehood = 0.0;
		DTW dtw = new DTW();
		//transform trajectory structure
		ArrayList<Integer[]> traj1_temp = new ArrayList<Integer[]>();
		for (int i = 0; i < traj1.GET001_length(); i++)
		{
			Integer[] a = {traj1.listPoint.get(i).x, traj1.listPoint.get(i).y}; 
			traj1_temp.add(a);
		}
		ArrayList<Integer[]> traj2_temp = new ArrayList<Integer[]>();
		for (int i = 0; i < traj2.GET001_length(); i++)
		{
			Integer[] a = {traj2.listPoint.get(i).x, traj2.listPoint.get(i).y}; 
			traj2_temp.add(a);
		}
		//count DTW distance
		likehood = dtw.DTWdistance(traj1_temp, traj2_temp);
		//record max distance
		if(likehood > maxDist) {
			maxDist = likehood;
		}
		return likehood;
	}
	
	public static double LCSS(Trajectory traj1, Trajectory traj2, int delta) {
		double result = 0.0;
		//transform trajectory structure
		ArrayList<Integer[]> traj1_temp = new ArrayList<Integer[]>();
		for (int i = 0; i < traj1.GET001_length(); i++)
		{
			Integer[] a = {traj1.listPoint.get(i).x, traj1.listPoint.get(i).y}; 
			traj1_temp.add(a);
		}
		ArrayList<Integer[]> traj2_temp = new ArrayList<Integer[]>();
		for (int i = 0; i < traj2.GET001_length(); i++)
		{
			Integer[] a = {traj2.listPoint.get(i).x, traj2.listPoint.get(i).y}; 
			traj2_temp.add(a);
		}
		
		LCSSdistance lcss = new LCSSdistance();
		lcss.setdelta(delta);
		result = lcss.LCSS_2D(traj1_temp, traj2_temp);
		
		int minSize = traj1.GET001_length();
		if(traj2.GET001_length() < minSize) {
			minSize = traj2.GET001_length();
		}
		
		return (double) (1 - (result / minSize));
		
	}
	
	public static void main(String[] args) {
		Trajectory t1 = new Trajectory();
		t1.ACT001_addPoint(1, 5);
		t1.ACT001_addPoint(1, 12);
		t1.ACT001_addPoint(1, 22);
		t1.ACT001_addPoint(1, 33);
		t1.ACT001_addPoint(1, 35);
		
		
		Trajectory t2 = new Trajectory();
		t2.ACT001_addPoint(1, 5);
		t2.ACT001_addPoint(1, 12);
		
		
		System.out.println(Matric.LCSS(t1, t2, 2));
	}
}
